package com.igelato.controller.Recharge;

import com.alibaba.fastjson2.JSONObject;
import com.igelato.response.R;
import com.igelato.service.RechargeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * ClassName: PayController
 * Package: com.tencent.wxcloudrun.controller
 * Description:
 *
 * @Date: 2023/1/22 8:34 AM
 * @Author: wht
 */
@Slf4j
@RestController

@RequestMapping("/2c")
public class RechargeController {

    @Autowired
    RechargeService rechargeService;

    @PostMapping("/recharge")
    public <T> R<T> recharge(@RequestHeader("X-WX-OPENID") String openid, @RequestBody Map body) {


        JSONObject payment = rechargeService.recharge(openid, body);

        return R.successData(payment);
    }


    @PostMapping("/recharge_callback")
    public JSONObject rechargeCallback(@RequestBody Map body) {
//判断是否支付成功
        JSONObject json = new JSONObject();
        if (body.get("returnCode").equals("SUCCESS")) {
//        更新订单状态
            rechargeService.rechargeCallback(body);
//返回
            json.put("errcode", 0);
            json.put("errmsg", "");
            return json;
        } else {
            json.put("errcode", 999);
            json.put("errmsg", "ERROR");
            return json;

        }

    }


}
